package com.tjc.command;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Actions {
    private static Map<String, Action> commandMap = new HashMap<>();

    public static void registerAction(Class<? extends Action> clazz) {
        try {
            Action action = clazz.getDeclaredConstructor().newInstance();
            Action.Command command = clazz.getAnnotation(Action.Command.class);
            commandMap.put(command.value(), action);
        } catch (Exception e) {
            throw new RuntimeException("register a command fail.", e);
        }
    }

    public static Action getAction(final String[] args) {
        if (args.length < 1) {
            usage();
            System.exit(1);
        }
        String commandName = args[0];
        Action action = commandMap.get(commandName);
        if (action == null) {
            throw new RuntimeException("cant find command:" + commandName);
        }

        // build args
        buildArgs(action, Arrays.copyOfRange(args, 1, args.length));

        return action;
    }

    private static void buildArgs(Action action, String[] args) {

    }

    public static void usage() {
    }
}
